﻿using System;
using System.Windows.Forms;
using HRM.DICTIONARY.DAL;
using EFERP;

namespace HRM.DICTIONARY.UI
{
    public partial class xucAllowance : DevExpress.XtraEditors.XtraUserControl
    {
        ICommon<dic_allowance> _exeDic;
        dic_allowance _currObj;
        private bool _isChooseRow;

        public xucAllowance()
        {
            InitializeComponent();
        }
        private void xucAllowance_Load(object sender, EventArgs e)
        {_exeDic = new Common<dic_allowance>(StaticVariable.ERPEn);
            _exeDic.GV = gbList;
            _exeDic.GC = gcList;
            _exeDic.ColIDName = "allowancecode";
            Reload();
        }

        private void Add()
        {
            xucAllowanceAdd frm = new xucAllowanceAdd(_exeDic);
            frm.ShowDialog();
            if (_exeDic.myAction == Actions.Insert || _exeDic.myAction == Actions.InsertMultiple)
            {
                _exeDic.myAction = Actions.View;
            }
            Reload();

        }
        private void Edit()
        {
            xucAllowanceAdd frm = new xucAllowanceAdd(_currObj,_exeDic);
            frm.ShowDialog();
            if (_exeDic.myAction == Actions.Update)
            {
                _exeDic.myAction = Actions.View;
                Reload();
            }
            _isChooseRow = false;
            DoShowOrHide(_isChooseRow);
        }
        private void Delete()
        {
            bool kq = _exeDic.DeleteOnGridView(_currObj);
            if (kq)
            {
                _isChooseRow = false;
                DoShowOrHide(_isChooseRow);
            }
            else
            {
                MessageBox.Show(@"Không thể xóa dữ liệu");
            }
        }
        private void Reload()
        {
            if (_exeDic != null)
            {
                _exeDic.AddGridControlFromList();
                _isChooseRow = false;
                _currObj = null;
                DoShowOrHide(_isChooseRow);
            }
        }
        public void Print()
        {
            if (gbList != null)
            {
                if (gbList.RowCount > 0)
                {
                    gbList.ShowPrintPreview();
                    _isChooseRow = false;
                    _currObj = null;
                    DoShowOrHide(_isChooseRow);
                }
            }
        }
        public void Export()
        {
            if (_exeDic != null)
            {
                _exeDic.AddGridExport(this);
                _isChooseRow = false;
                _currObj = null;
                DoShowOrHide(_isChooseRow);
            }
        }
        private void DoShowOrHide(bool isChoose)
        {
            bbiEdit.Enabled = isChoose;
            bbiDelete.Enabled = isChoose;
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Escape:
                    Dispose();
                    return true;

                case Keys.F1:
                    //Help Link Click
                    return true;

                case Keys.F5:
                    this.Reload();
                    return true;

                case (Keys.Control | Keys.E):
                    this.Export();
                    return true;

                case (Keys.Control | Keys.F):
                    //Search
                    return true;

                case (Keys.Control | Keys.N):
                    Add();
                    return true;

                case (Keys.Enter):
                    Edit(); 
                    return true;

                case (Keys.Control | Keys.X):
                    Delete();
                    return true;

                case (Keys.Control | Keys.P):
                    this.Print();
                    return true;

                case (Keys.Alt | Keys.X):
                    Dispose();
                    return true;
            }
            return false;
        }

        private void bbiAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Add();
        }

        private void bbiEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit();
        }
        private void bbiDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Delete();
        }
        private void bbiRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Reload();
        }
        private void bbiPrint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Print();
        }
        private void bbiExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Export();
        }
        private void bbiClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Dispose();
        }
        
        private void gbList_Click(object sender, EventArgs e)
        {
            _currObj = _exeDic.gbList_Click(sender, e);
            if (_currObj == null)
            {
                MessageBox.Show(@"Vui lòng chọn dòng trên bảng tính");
            }
            else
            {
                _isChooseRow = true;
                DoShowOrHide(_isChooseRow);
            }
        }
        private void gbList_DoubleClick(object sender, EventArgs e)
        {
            _currObj = _exeDic.gbList_Click(sender, e);
            if (_currObj == null)
            {
                MessageBox.Show(@"Vui lòng chọn dòng trên bảng tính");
            }
            else
            {
                Edit();
            }
        }

        public void xucPosition_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.Escape:
                    Dispose();
                    break;

                case Keys.F1:
                    //Help Link Click

                case Keys.F5:
                    this.Reload();
                    break;

                case (Keys.Control | Keys.E):
                    this.Export();
                    break;

                case (Keys.Control | Keys.F):
                    //Search
                    break;

                case (Keys.Control | Keys.N):
                    //Call Form Add
                    break;

                case (Keys.Control | Keys.P):
                    this.Print();
                    break;

                case (Keys.Alt | Keys.D):
                    MessageBox.Show(@"You are master ");
                    break;

                case (Keys.Alt | Keys.X):
                    Dispose();
                    break;
            }
        }
    }
}
